package com.eqdd.yiqidian.utils;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author吕志豪 .
 * @date 18-1-13  上午9:28.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class PayUtil {

    private static final String WX_APPID = "wx225929aa256e2053";

    public static void pay(BaseActivity baseActivity, String body, String subject, String totalAmount, String productCode, String outTradeNo) {
        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.ALIPAY)
                .params("Body", body)
                .params("Subject", subject)
                .params("TotalAmount", totalAmount)
                .params("ProductCode", productCode)
                .params("OutTradeNo", outTradeNo)
                .params("userGuid", AppConstent.user.getGuid())
                .execute(new DialogCallBack<HttpResult<String>>(baseActivity) {
                    @Override
                    public void onSuccess(Response<HttpResult<String>> response) {
                        HttpResult<String> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            Schedulers.newThread().createWorker().schedule(() -> {
                                PayTask alipay = new PayTask(baseActivity);
                                Map<String, String> result = alipay.payV2(httpResult.getItems(), true);
                                AndroidSchedulers.mainThread().createWorker().schedule(() -> {
                                    PayResult payResult = new PayResult(result);
                                    /**
                                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                                     */
                                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                                    String resultStatus = payResult.getResultStatus();
                                    // 判断resultStatus 为9000则代表支付成功
                                    if (TextUtils.equals(resultStatus, "9000")) {
                                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                                        Toast.makeText(baseActivity, "支付成功", Toast.LENGTH_SHORT).show();
                                    } else {
                                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                                        Toast.makeText(baseActivity, "支付失败", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            });
                        }
                    }
                });
    }


    public static void aliPay(Activity activity, String body, String subject, String OrderType, int funcType, String startTime, String years) {
        User user = AppConstent.user;
        if (user.getGuid() == null || user.getCompanyId() == 0) {
            Toast.makeText(activity, "支付失败", Toast.LENGTH_SHORT).show();
            return;
        }

        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.ALI_APPLY)
                .params("Body", body)
                .params("Subject", subject)
                .params("OrderType", OrderType)
                .params("companyId", user.getCompanyId())
                .params("userGuid", user.getGuid())
                .params("funcType", funcType)
                .params("statrTime", startTime)
                .params("years", years)
                .execute(new DialogCallBack<HttpResult<String>>(activity) {
                    @Override
                    public void onSuccess(Response<HttpResult<String>> response) {
                        HttpResult<String> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            Schedulers.newThread().createWorker().schedule(() -> {
                                PayTask alipay = new PayTask(activity);
                                Map<String, String> result = alipay.payV2(httpResult.getItems(), true);
                                AndroidSchedulers.mainThread().createWorker().schedule(() -> {
                                    PayResult payResult = new PayResult(result);
                                    /**
                                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                                     */
                                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                                    String resultStatus = payResult.getResultStatus();
                                    // 判断resultStatus 为9000则代表支付成功
                                    if (TextUtils.equals(resultStatus, "9000")) {
                                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                                        Toast.makeText(activity, "支付成功", Toast.LENGTH_SHORT).show();
                                    } else {
                                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                                        Toast.makeText(activity, "支付失败", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            });
                        }
                    }
                });
    }


    public static void wxPay(Activity activity, String body, String attach, String amount, String orderType,
                             String startTime, String funcType, int years) {
        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.WXPAY)
                .params("Body", body)
                .params("attach", attach)
                .params("TotalAmount", amount)
                .params("OrderType", orderType)
                .params("startTime", startTime)
                .params("funcType", funcType)
                .params("years", years)
                .params("userGuid", AppConstent.user.getGuid())
                .execute(new DialogCallBack<HttpResult<String>>(activity) {
                    @Override
                    public void onSuccess(Response<HttpResult<String>> response) {
                        HttpResult<String> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {

                        }
                    }
                });


        IWXAPI wxApi = WXAPIFactory.createWXAPI(activity, WX_APPID, true);
        wxApi.registerApp(WX_APPID);
        PayReq request = new PayReq();
        request.appId = WX_APPID;
        request.partnerId = "1900000109";
        request.prepayId = "1101000000140415649af9fc314aa427";
        request.packageValue = "Sign=WXPay";
        request.nonceStr = "1101000000140429eb40476f8896f4c9";
        request.timeStamp = "1398746574";
        request.sign = "7FFECB600D7157C5AA49810D2D8F28BC2811827B";
        wxApi.sendReq(request);

    }
}
